package com.fit.dao.homeworkRecords;

import java.util.List;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.homeworkRecords.HomeworkRecords;
import com.fit.entities.user.User;

public class HomeworkRecordsDAOImpl implements HomeworkRecordsDAO {

	@Inject
	private Session session;

	@Log
	public boolean save(HomeworkRecords newHomeworkRecords) {
		if (newHomeworkRecords != null) {
			session.save(newHomeworkRecords);
			return true;
		}
		return false;
	}

	@Log
	public boolean delete(HomeworkRecords homeworkRecords) {
		if (homeworkRecords != null) {
			session.delete(homeworkRecords);
			return true;
		}
		return false;
	}

	@Log
	public boolean update(HomeworkRecords homeworkRecords) {
		if (homeworkRecords != null) {
			session.update(homeworkRecords);
			return true;
		}
		return false;
	}

	@Log
	public List findAll() {
		return session.createCriteria(HomeworkRecords.class).list();
	}

	@Log
	public boolean deleteByID(Long id) {
		HomeworkRecords u = findByID(id);
		if (u != null) {
			delete(u);
			return true;
		}
		return false;
	}

	@Log
	public HomeworkRecords findByID(Long id) {
		if (id != null) {
			return (HomeworkRecords) session.get(HomeworkRecords.class, id);
		}
		return null;
	}

	@Log
	public HomeworkRecords findByIdHomeworkAndIdStudent(int homeworkId, int studentId) {
		if (homeworkId == 0 || studentId == 0) {
			return null;
		}

		if (homeworkId==(0) || studentId==(0)) {
			return null;
		}

		List l = session.createCriteria(HomeworkRecords.class)
				.add(Restrictions.eq("studentId", studentId))
				.add(Restrictions.eq("homeworkId", homeworkId)).list();
		HomeworkRecords u = null;
		if (l != null) {
			if (l.size() > 0) {
				u = (HomeworkRecords) l.get(0);
			}
		}
		return u;
	}

}
